package wjh.service;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import wjh.listenner.RecordsListenner;
import wjh.mapper.DoctorMapper;
import wjh.pojo.Doctor;
import wjh.pojo.Plan;
import wjh.pojo.Records;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 功能
 * 作者：lyh
 * 日期：2024/9/3 17:16
 */
@Service
public class DoctorServiceImpl implements DoctorService{
    @Autowired
    private DoctorMapper doctorMapper;
    @Override
    public List<Doctor> findDoctorsKe() {
        return doctorMapper.findDoctorsKe();
    }

    @Override
    public List<Doctor> findDoctors2() {
        return doctorMapper.findDoctors2();
    }

    @Override
    public void updatePaiBan(Plan plan) {
        doctorMapper.updatePaiBan(plan);
    }

    @Override
    public List<Records> findAllRecord() {
        return doctorMapper.findAllRecord();
    }

    @Override
    public List<Records> findAllRecordsLike(String likeName) {
        return doctorMapper.findAllRecordsLike(likeName);
    }

    //导出
    @Override
    public void exportRecords(Integer pageNum, Integer pageSize, HttpServletResponse response,String likeName) {
        try {
            //响应类型  以下载的形式下载excel
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            //设置文件名称
            String fileName = URLEncoder.encode("第"+pageNum+"页的"+pageSize+"条病历", "UTF-8");
            //设置响应头 下载的方式
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            //分页查询
            PageHelper.startPage(pageNum,pageSize);
//            List<Records> recordList = doctorMapper.findAllRecord();
            List<Records> recordList = doctorMapper.findAllRecordsLike(likeName);
            PageInfo<Records> pageInfo = new PageInfo<>(recordList);
            //将文件转换成输出响应流 可以以附件的形式下载
            EasyExcel.write(response.getOutputStream(),Records.class).sheet("第"+pageNum+"页的"+pageSize+"条病历")
                    .doWrite(pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Autowired
    private RecordsListenner recordsListenner;
    @Override
    public void importRecords(MultipartFile file, HttpSession session) {
        try {
            //读取之前清空session域和集合
            recordsListenner.getList().clear();
            session.removeAttribute("recordList2");
            //将file对象转换成输入流
            EasyExcel.read(file.getInputStream(),Records.class,recordsListenner).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
